<!--
 * @Author: Shususumu
 * @Date: 2020-10-15 15:57:45
 * @LastEditTime: 2020-10-20 13:56:33
 * @FilePath: \campusPTjobs\src\views\category\index.vue
-->
<template>
  <div id="category">
    <!-- 控件 -->
    <div id="control">
      <el-button
        type="primary"
        size="small"
        @click="(dialogFormVisible = true), addmsg()"
        >添加</el-button
      >
      <el-input
        placeholder="请输入查询的内容"
        v-model="input"
        class="input"
        clearable
        size="small"
      >
      </el-input>
      <el-button type="primary" size="small" @click="search()">查询</el-button>
    </div>
    <!-- 表格 -->
    <template>
      <el-table :data="categoryList" stripe style="width: 100%">
        <el-table-column fixed prop="id" label="编号"> </el-table-column>
        <el-table-column fixed prop="name" label="栏目名称"> </el-table-column>
        <el-table-column prop="num" label="序号" width="120"> </el-table-column>
        <el-table-column prop="parentName" label="父栏目"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="dlt(scope.row)" type="text" size="small"
              >删除</el-button
            >
            <el-button
              @click="(dialogFormVisible = true), udt(scope.row)"
              type="text"
              size="small"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[6, 8, 10, 12]"
      :page-size="6"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="categoryform">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
            clearable
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="num">
          <el-input
            v-model.number="form.num"
            autocomplete="off"
            clearable
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属栏目" :label-width="formLabelWidth">
          <el-select
            v-model="form.parentId"
            placeholder="请选择"
            size="small"
            clearable
          >
            <el-option
              v-for="item in parentctg"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="(dialogFormVisible = false), saveOrUpdate('categoryform')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import qs from "querystring";
import { log } from "util";
export default {
  data() {
    return {
      categorys: [],
      parentctg: [],
      input: "",
      dialogFormVisible: false,
      title: "修改栏目信息",
      form: {
        id: "",
        name: "",
        num: "",
        parentId: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入栏目名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        num: [
          { required: true, message: "序号不能为空" },
          { type: "number", message: "序号必须为数字值" },
        ],
      },
      formLabelWidth: "100px",
      fileList: [],
      currentPage: 1,
      total: 1,
      pageSize: 6,
    };
  },
  computed: {
    categoryList() {
      var self = this;
      // this.categorys->所有产品
      //   console.log(this.categorys, "产品");
      //查询所有category
      //   console.log(this.parentctg, "栏目");
      // id匹配category
      this.categorys.forEach((item) => {
        // item->每一个产品信息
        for (var i = 0; i < self.parentctg.length; i++) {
          if (item.parentId == self.parentctg[i].id) {
            // console.log(self.categorys[i]);
            item.parentName = self.parentctg[i].name;
          }
        }
      });
      return this.categorys;
    },
  },
  methods: {
    // 全局封装分页查询
    findquery(name) {
      if (name) {
        var pageobj = {
          page: this.currentPage - 1,
          pageSize: this.pageSize,
          name: name,
        };
      } else {
        var pageobj = { page: this.currentPage - 1, pageSize: this.pageSize };
      }
      pageobj = qs.stringify(pageobj);
      // console.log(pageobj);
      this.$axios.post("/category/query", pageobj).then((res) => {
        // console.log(res);
        this.categorys = res.data.data.list;
        this.total = res.data.data.total;
      });
      // this.$axios.get("/category/findAll").then((res) => {
      //   console.log(res);
      // });
    },
    //封装栏目查询
    findcategory() {
      return this.$axios.get("/category/findAll");
    },
    //查询
    search() {
      if (this.input) {
        this.findquery(this.input);
      } else {
        this.findquery();
      }
    },
    //删除
    dlt(row) {
      var id = row.id;
      this.$confirm("此操作将永久删除该产品, 是否继续?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get("category/deleteById", { params: { id } })
            .then((res) => {
              // console.log(res.data);
              if (res.data.status == 200) {
                this.$message({
                  type: "success",
                  message: res.data.message,
                });
                this.findquery();
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败,绑定了其他数据",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页功能
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.findquery();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.findquery();
    },
    //编辑按钮功能
    udt(row) {
      // console.log(row);
      this.title = "修改栏目信息";
      this.form = {
        id: row.id,
        name: row.name,
        num: row.num,
        parentId: row.parentId,
      };
    },
    //新增按钮功能
    addmsg() {
      this.title = "新增栏目信息";
      this.form = {};
    },
    //提交按钮功能
    saveOrUpdate(formName) {
      var self = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //表单验证成功-获取用户输入-》发送保存请求
          self.$axios
            .post("/category/saveOrUpdate", qs.stringify(self.form))
            .then((res) => {
              if (res.data.status == 200) {
                //保存成功
                //1.关闭模态框

                //2.刷新页面
                self.findquery();
                //3.提示用户保存成功-弹框
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
              } else {
                //保存失败
                this.$message.error("保存失败");
              }
            });
        } else {
          //表单验证失败
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.findcategory().then((res) => {
      this.parentctg = res.data.data;
      //   console.log(this.parentctg);
      this.findquery();
    });
  },
};
</script>
<style lang="scss" scoped>
#category {
  margin: 18px;
}
.el-input {
  width: 200px;
  margin-left: 20px;
}
</style>